package com.hdax.gulimall.member.service.impl;

import com.hdax.gulimall.member.dao.MemberLevelDao;
import com.hdax.gulimall.member.entity.MemberLevelEntity;
import com.hdax.gulimall.member.exception.PhoenNameExistExeception;
import com.hdax.gulimall.member.exception.UserNameExistExeception;
import com.hdax.gulimall.member.vo.MemberLoginVo;
import com.hdax.gulimall.member.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.common.utils.PageUtils;
import com.hdax.common.utils.Query;

import com.hdax.gulimall.member.dao.MemberDao;
import com.hdax.gulimall.member.entity.MemberEntity;
import com.hdax.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {


    @Autowired
    private MemberLevelDao memberLevelDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 完成会员的注册
     * @param memberRegistVo
     */
    @Override
    public void regist(MemberRegistVo memberRegistVo) {
        MemberEntity member=new MemberEntity();
        //会员等级的id
        MemberLevelEntity entity=memberLevelDao.memberLevel();
        member.setLevelId(entity.getId());
        //检查手机号和用户名是否唯一
        checkEmailUnique(memberRegistVo.getPhone());
        checkUserNameUnique(memberRegistVo.getUserName());
        member.setMobile(memberRegistVo.getPhone());
        member.setUsername(memberRegistVo.getUserName());
        //密码(加密存储)
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(memberRegistVo.getPassword());
        member.setPassword(encode);
        int insert = this.baseMapper.insert(member);
    }

    /**
     * 检查手机号是否是唯一
     * @param phone
     * @return
     */
    @Override
    public void checkEmailUnique(String phone) {
        Integer integer = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if(integer>0){
            throw new PhoenNameExistExeception();
        }
    }

    /**
     * 检查用户名是否唯一
     * @param userName
     * @return
     */
    @Override
    public void checkUserNameUnique(String userName) {
        Integer integer = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if(integer>0){
            throw new UserNameExistExeception();
        }
    }

    /**
     * 用户的登录功能
     *
     * @param memberLoginVo
     * @return
     */
    @Override
    public MemberEntity login(MemberLoginVo memberLoginVo) {
        String loginacct = memberLoginVo.getLoginacct();
        String password = memberLoginVo.getPassword();
        MemberEntity member = this.baseMapper.selectOne
                (new QueryWrapper<MemberEntity>().eq("username", loginacct).or().eq("mobile", loginacct));
        if(member==null){
            //登录失败
            return null;
        }else {
            String password1 = member.getPassword();
            BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();

            boolean matches = bCryptPasswordEncoder.matches(password, password1);
            if(matches){
                //密码匹配返回对象，否则都是返回null
                return member;
            }else {
                return null;
            }
        }
    }

}